const {
  GiftShopModel
} = require('../../utils/giftshop-model.js');
var giftShopModel = new GiftShopModel();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    data: {
      type: Object,
      value: {},
      observer: function (newVal, oldVal) {
        if (newVal) {
          this.setData({
            provinceName: newVal.provinceName || '', //省
            provinceCode: newVal.provinceCode || '', //省
            cityName: newVal.cityName || '', //市
            cityCode: newVal.cityCode || '', //市
            districtName: newVal.districtName || '', //区
            districtCode: newVal.districtCode || '', //区
            streetName: newVal.streetName || '', //街道
            streetCode: newVal.streetCode || '', //街道
            cityShow:newVal.cityShow,
          })
          if(newVal.cityShow){
            this.getCityData()
            this.filterData()
          }
        }
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    provinceName: '', //省
    provinceCode: '', //省
    cityName: '', //市
    cityCode: '', //市
    districtName: '', //区
    districtCode: '', //区
    streetName: '', //街道
    streetCode: '', //街道
    moduleShow: null, //1、省，2、市，3、区，4、街道
    provincelist: [], //省
    cityList: [], //市
    districtList: [], //区
    streetList: [], //街道
    anchorList: [], //右侧锚点
    jumpNum: '', //跳转到那个字母
    myRegion: {}, //向父级传递参数
    areaHot: [] //热门城市
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 获取省市区地址的接口
     */
    getAddressData: function (addressCode, type, isSelectCity) {
      let that = this;
      giftShopModel.getAreaLocalInfo({
        merchantId: wx.getStorageSync('merchantId'),
        parentAddressId: addressCode //如果获得一级输入null
      }).then(data => {
        if (type == 1) {
          that.setData({
            provincelist: data || [],
            moduleShow: 1,
          })
        } else if (type == 2) {
          that.setData({
            cityList: data || [],
            moduleShow: 2,
          })
        } else if (type == 3) {
          that.setData({
            districtList: data || [],
            moduleShow: 3,
          })
        } else if (type == 4) {
          that.setData({
            streetList: data || [],
            moduleShow: (data && data.length > 0) ? 4 : 3,
          })
          if (data && !data.length > 0 && isSelectCity) {
            this.selectRegionData();
          }
        }
        that.getIndexCharacter(data)
      }).catch(err => {
        wx.showToast({
          title: `${err.error}`,
          icon: 'none',
          duration: 2000
        })
      })
    },

    /**
     * 热门城市
     */
    getCityData: function () {
      giftShopModel.areaHot({
        merchantId: wx.getStorageSync('merchantId')
      }).then(data => {
        this.setData({
          areaHot: data || []
        })
      }).catch(err => {
        wx.showToast({
          title: `${err.error}`,
          icon: 'none',
          duration: 2000
        })
      })
    },
    /**
     * 选择热门城市
     */
    hotCity(e) {
      let detail = e.currentTarget.dataset.detail;
      if(this.data.provinceCode!=detail.provinceCode){
        this.setData({
          cityName: '', //市
          cityCode: '', //市
          cityList: [], //市
          districtName: '', //区
          districtCode: '', //区
          districtList: [], //区
          streetName: '', //街道
          streetCode: '', //街道
          streetList: [], //街道
          anchorList: [], //右侧锚点
        })
      }
      if (detail.provinceCode == detail.addressCode) {
        this.setData({
          provinceCode: detail.provinceCode,
          provinceName: detail.provinceName
        })
        this.getAddressData(detail.addressCode, 2)
      } else {
        this.setData({
          provinceCode: detail.provinceCode,
          provinceName: detail.provinceName,
          cityName: detail.addressName, //市
          cityCode: detail.addressCode, //市
        })
        this.getAddressData(detail.addressCode, 3)
      }
    },
    /**
     * 已选择省市区街道
     */
    filterData() {
      let that=this;
      if (that.data.streetCode && that.data.streetCode != '' && that.data.streetCode != 0) {
        that.getAddressData(that.data.districtCode, 4)
      } else if (that.data.districtCode && that.data.districtCode != '') {
        that.getAddressData(that.data.cityCode, 3)
      } else {
        that.getAddressData(null, 1)
      }
      var myRegion = {
        provinceName: that.data.provinceName, //省
        provinceCode: that.data.provinceCode, //省
        cityName: that.data.cityName, //市
        cityCode: that.data.cityCode, //市
        districtName: that.data.districtName, //区
        districtCode: that.data.districtCode, //区
        streetName: that.data.streetName, //街道
        streetCode: that.data.streetCode, //街道
      }
      that.setData({
        myRegion: myRegion,
      })
    },

    /**
     * 省
     * @param {*} e 
     */
    selectProvince(e) {
      let detail = e.currentTarget.dataset.detail;
      if (this.data.provinceCode !== detail.addressCode) {
        this.setData({
          cityName: '', //市
          cityCode: '', //市
          cityList: [], //市
          districtName: '', //区
          districtCode: '', //区
          districtList: [], //区
          streetName: '', //街道
          streetCode: '', //街道
          streetList: [], //街道
          anchorList: [], //右侧锚点
        })
      }
      // 点击选择当前
      this.setData({
        provinceName: detail.addressName, //省
        provinceCode: detail.addressCode, //省
      })
      this.getAddressData(detail.addressCode, 2)
    },
    /**
     * 市
     * @param {*} e 
     */
    selectCity(e) {
      let detail = e.currentTarget.dataset.detail;
      if (this.data.cityCode !== detail.addressCode) {
        this.setData({
          districtName: '', //区
          districtCode: '', //区
          districtList: [], //区
          streetName: '', //街道
          streetCode: '', //街道
          streetList: [], //街道
          anchorList: [], //右侧锚点
        })
      }
      // 点击选择当前
      this.setData({
        cityName: detail.addressName, //市
        cityCode: detail.addressCode, //市
      })
      this.getAddressData(detail.addressCode, 3)
    },
    /**
     * 区
     * @param {*} e 
     */
    selectArea(e) {
      let detail = e.currentTarget.dataset.detail;
      if (this.data.districtCode !== detail.addressCode) {
        this.setData({
          streetName: '', //区
          streetCode: '', //区
          streetList: [], //区
          anchorList: [], //右侧锚点
        })
      }
      // 点击选择当前
      this.setData({
        districtName: detail.addressName, //区
        districtCode: detail.addressCode, //区
      })
      this.getAddressData(detail.addressCode, 4, true)
    },
    /**
     * 街道
     * @param {*} e 
     */
    selectStreet(e) {
      let detail = e.currentTarget.dataset.detail;
      // 点击选择当前
      this.setData({
        streetName: detail.addressName, //街道
        streetCode: detail.addressCode, //街道
      })
      this.selectRegionData();
    },

    /**
     * 右侧锚点
     */
    getIndexCharacter(data) {
      const anchorData = (data || []).map(item => {
        return {
          id: item.indexCharacter,
        }
      });
      this.setData({
        anchorList: anchorData,
      })
    },
    // 右侧字母点击事件
    jumpMt(e) {
      let jumpNum = e.currentTarget.dataset.id;
      this.setData({
        jumpNum: jumpNum
      });
    },
    /**
     * 选择省市区街道
     */
    getSelectId: function (e) {
      let id = e.currentTarget.dataset.id;
      let code = e.currentTarget.dataset.code;
      this.getAddressData(code, id)
    },

    /**
     * 选择数据
     */
    selectRegionData() {
      var myRegion = {
        provinceName: this.data.provinceName, //省
        provinceCode: this.data.provinceCode, //省
        cityName: this.data.cityName, //市
        cityCode: this.data.cityCode, //市
        districtName: this.data.districtName, //区
        districtCode: this.data.districtCode, //区
        streetName: this.data.streetName, //街道
        streetCode: this.data.streetCode, //街道
      }
      let myEventOption = {
        bubbles: false, //事件是否冒泡
        composed: false, //事件是否可以穿越组件边界
        capturePhase: false //事件是否拥有捕获阶段
      } // 触发事件的选项
      this.setData({
        myRegion: myRegion,
        cityShow: false, //弹出层
      })
      this.triggerEvent('detail', myRegion, myEventOption);
    },

    /**
     * 关闭弹出层
     */
    closeLayer() {
      let that = this;
      that.setData({
        provinceName: '', //省
        provinceCode: '', //省
        cityName: '', //市
        cityCode: '', //市
        districtName: '', //区
        districtCode: '', //区
        streetName: '', //街道
        streetCode: '', //街道
        provinceList: [], //省
        cityList: [], //市
        districtList: [], //区
        streetList: [], //街道
        anchorList: [], //右侧锚点
        cityShow: false, //弹出层
        moduleShow: null,
      }, function () {
        let myEventOption = {
          bubbles: false, //事件是否冒泡
          composed: false, //事件是否可以穿越组件边界
          capturePhase: false //事件是否拥有捕获阶段
        } // 触发事件的选项
        this.triggerEvent('detail', that.data.myRegion, myEventOption);
      })
    },

    /**
     * 父级传参
     */
    parentData(myRegion) {
      let myEventOption = {
        bubbles: false, //事件是否冒泡
        composed: false, //事件是否可以穿越组件边界
        capturePhase: false //事件是否拥有捕获阶段
      } // 触发事件的选项
      this.setData({
        myRegion: myRegion,
        cityShow: false, //弹出层
      })
      this.triggerEvent('detail', myRegion, myEventOption);
    }
  }
})